import { assert, beforeAll, describe, test } from 'vitest';
import { CHECK_MODE, testCaseCheck } from '../common/testCommon';
import InsecureRandomDsl from '../../../../src/checker/dsl/InsecureRandomDsl';
import path from 'path';
import { CheckEntry, Rule } from '../../../../src/Index';

let realPath: string = '';
let checkEntry: CheckEntry;

beforeAll(async () => {
  const rule: Rule = new Rule('');
  checkEntry = await testCaseCheck(
    './test/unittest/sample/InsecureRandomDsl',
    rule,
    CHECK_MODE.FILE2CHECK,
    InsecureRandomDsl,
    false,
    true
  );
  realPath = checkEntry.scene.getRealProjectDir();
});

describe('InsecureRandomDslTest', () => {
  test('InsecureRandomDslTest_001', () => {
    const detectFile = path.join(realPath, 'InsecureRandomDslReport.ts');
    const expectReportList = ['2%16', '6%11']; // 这里根据实际样例代码中的 Math.random() 出现位置填写
    const detectFileReport = checkEntry.fileChecks.find(fileCheck => fileCheck.arkFile.getFilePath() === detectFile);

    assert.isDefined(detectFileReport, 'The file path is error.');
    assert.equal(
      detectFileReport?.issues.length,
      expectReportList.length,
      'The number of reported line is different from the expected number of line.'
    );

    detectFileReport?.issues.forEach((issue, index) => {
      assert.include(issue.defect.fixKey, expectReportList[index]);
    });
  });

  test('InsecureRandomDslTest_002', () => {
    const detectFile = path.join(realPath, 'InsecureRandomDslNoReport.ts');
    const detectFileReport = checkEntry.fileChecks.find(fileCheck => fileCheck.arkFile.getFilePath() === detectFile);
    assert.equal(detectFileReport?.issues.length, 0, 'The number of reported line should be 0.');
  });
});
